package cn.est.controller;
import cn.est.pojo.Brand;
import cn.est.constants.Constants;
import cn.est.dto.Page;
import cn.est.dto.Result;
import cn.est.service.BrandService;
import cn.est.utils.EmptyUtils;
import cn.est.utils.ResultUtils;
import javax.annotation.Resource;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import java.util.Map;

@Controller
@RequestMapping("/client/brand")
public class BrandController {

    @Resource
    private BrandService brandService;

    @RequestMapping("/search")
    @ResponseBody
    public Page<Brand> searchBrand(@RequestParam Map<String,Object> param) {
        Page<Brand> brandPage = null;
        try {
            Integer pageNo= EmptyUtils.isEmpty(param.get("pageNo"))? Page.DEFAULT_PAGE_NO:Integer.parseInt((String) param.get("pageNo"));
            Integer pageSize= EmptyUtils.isEmpty(param.get("pageSize"))? Page.DEFAULT_PAGE_SIZE:Integer.parseInt((String) param.get("pageSize"));
            brandPage  = brandService.queryBrandPageByMap(param, pageNo, pageSize);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return brandPage;
    }

    @RequestMapping("/saveOrUpdateBrand")
    @ResponseBody
    public Result saveOrUpdateBrand(Brand brand){
        Integer flag=0;
        try {
            if(EmptyUtils.isEmpty(brand.getId())){
                flag=brandService.qdtxAddBrand(brand);
            }else{
                flag=brandService.qdtxModifyBrand(brand);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return flag>=1?ResultUtils.returnSuccess():ResultUtils.returnFail();
    }

    @RequestMapping("/getBrandById")
    @ResponseBody
    public Result getBrandById(Long id){
        Result result= ResultUtils.returnSuccess();
        Brand brand=null;
        try {
            brand=brandService.getBrandById(id);
            result.setData(brand);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    @RequestMapping("/batchRemoveBrand")
    @ResponseBody
    public Result batchRemoveBrand(String ids){
        Integer flag=0;
        try {
            flag=brandService.qdtxBatchDeleteBrand(ids);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResultUtils.returnSuccess();
    }

    @RequestMapping("/removeBrandById")
    @ResponseBody
    public Result removeBrandById(Long id){
        Integer flag=0;
        try {
            flag=brandService.qdtxDeleteBrandById(id);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResultUtils.returnSuccess();
    }
}
